import wmi
import os
import socket
import sys
import winreg
#TODO xp系统不能读取内存厂家
#TODO 厂家信息都为英文或数字存在不准确
w = wmi.WMI()#获取电脑使用者信息
# 主板日期
# for bios in w.Win32_BIOS():
#     print(bios)
# for processor in w.Win32_Processor():
#     print(processor)
def getBios():
    for bios in w.Win32_BIOS():
        return bios.ReleaseDate
# 主板序列号
def getBoardId():
    for board_id in w.Win32_BaseBoard():
        return board_id.Manufacturer+' '+board_id.Product
#获取cpu信息
def getCpu():
    for processor in w.Win32_Processor():
        cpuname = processor.Name
        return processor.Name.strip()
#获取硬盘信息
def getDisk():
    disks = []
    for disk in w.Win32_DiskDrive():
        tmpmsg = {}
        tmpmsg['Caption'] = disk.Caption
        tmpmsg['size'] = int(int(disk.size)/1000/1000/1000)
        tmpmsg['InterfaceType'] = disk.InterfaceType
        disks.append(tmpmsg)
    return disks
#获取显卡信息
def getVideo():
    for video in w.Win32_VideoController():
	    if video.Availability == 3:
	        return(video.caption)
#获取内存信息
def getMemModule():
    memArr = []
    mName = []
    for memModule in w.Win32_PhysicalMemory():
        meminfo = {}
        mNames = {}
        mNames = memModule.manufacturer
        meminfo['speed'] = memModule.speed
        meminfo['capacity']  = int(int(memModule.Capacity)/1024**3)
        memArr.append(meminfo)
        mName.append(mNames)
    return memArr,mName
#获取桌面路径
def getDeskP():#XP系统不可用
    return os.path.join(os.path.expanduser('~'),"Desktop")
def getDesktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')#利用系统的链表
    return winreg.QueryValueEx(key, "Desktop")[0] #返回的是Unicode类型数据

if __name__ == "__main__":
    init = getDisk()
    for ini in init:
        if ini['InterfaceType'] == "USB":
            print("拔掉U盘重新启动程序")
            os.system('pause')
            sys.exit(1)

desktop = getDesktop()
fo = os.open(desktop+'\\硬件信息.txt',os.O_RDWR|os.O_CREAT)
print("请打开桌面上的‘硬件信息.txt’将里面的参数信息内容复制到信息收集表对应的表格中！")
print("\n")
print("开始生成设备信息！")
strzb = '--------[ 主板 ]--------------------------------------------------------------------------------'+"\n\n"
strzb = strzb.encode()
os.write(fo,strzb)
str1 = "主板型号的填写参数："+str(getBoardId())+"\n"
str1 = str1.encode()
os.write(fo,str1)
str10 = "电脑投用日期："+str(getBios()[0:4])+"-"+str(getBios()[4:6])+"-"+str(getBios()[6:8])+"\n\n"
str10 = str10.encode()
os.write(fo,str10)
print("主板信息生成成功")


strxk = '--------[ 显卡 ]--------------------------------------------------------------------------------'+"\n\n"
strxk = strxk.encode()
os.write(fo,strxk)
str2 = "主显卡型号的填写参数："+str(getVideo())+"\n\n"
str2 = str2.encode()
os.write(fo,str2)
print("显卡信息生成成功")

strnc = '--------[ 内存 ]--------------------------------------------------------------------------------'+"\n\n"
strnc = strnc.encode()
os.write(fo,strnc)
str3 = getMemModule()

capNum = 0
for i in str3[0]:
    capNum += i['capacity']
str41 = "内存大小填写参数："+str(capNum)+"GB"+"\n"
str41 = str41.encode()
os.write(fo,str41)
str42 = "数量填写参数："+str(len(str3[0]))+"\n"
str42 = str42.encode()
os.write(fo,str42)
str43 = "内存频率："+str(str3[0][0]['speed'])+"MHz"+"\n\n"
str43 = str43.encode()
os.write(fo,str43)
print("内存信息生成成功")

stryp = '--------[ 硬盘 ]--------------------------------------------------------------------------------'+"\n\n"
stryp = stryp.encode()
os.write(fo,stryp)
distinfo = getDisk()
num = 1
for d in distinfo:
    if d['size'] >= 1000:
        d['size'] = "1TB"
        #TODO判断2t4t内存
    else:
        d['size'] = str(d['size'])+"GB"
    strcp = "第"+str(num)+"块硬盘产品型号填写参数："+d['Caption'].strip(' SCSI Disk Device')+"    容量为："+d['size']+"\n"
    strcp = strcp.encode()
    os.write(fo,strcp)
    num = num+1
kg = "\n"
kg = kg.encode()
os.write(fo,kg)
print("硬盘信息生成成功")

print("处理器信息生成成功")
strcpu = '--------[ cpu ]--------------------------------------------------------------------------------'+"\n\n"
strcpu = strcpu.encode()
os.write(fo,strcpu)
str40 = "处理器型号的填写参数："+getCpu()
str40 = str40.encode()
os.write(fo,str40)

print("硬件信息.txt文件已在桌面生成！")
os.system('pause')


